//
//  ZYCommentContentController.m
//  DDD
//
//  Created by Ethan on 2017/10/6.
//  Copyright © 2017年 乾坤. All rights reserved.
//

#import "ZYCommentContentController.h"
#import "ZYCommentContentCell.h"
#import "ZYCommentContentTowCell.h"
#import "ZYCommentContentThreeCell.h"
#import "ZYAddEssentialController.h"
#import "ZYCommentContentModel.h"
#import "ZYCommentContentTowModel.h"
#import "ZYCommentContentThreeModel.h"
#define kCell @"cell"
#define kCelll @"celll"
#define kCellll @"cellll"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define UIColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
@interface ZYCommentContentController ()<UITableViewDelegate,UITableViewDataSource>
{
    int _num;
    int _numTow;
    int _numThree;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy)NSString *str;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) NSMutableArray *dataTowSource;
@property (nonatomic,strong) NSMutableArray   *dataThreeSource;

@end

@implementation ZYCommentContentController

- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
}

- (NSMutableArray *)dataTowSource {
    
    if (_dataTowSource == nil) {
        self.dataTowSource = [NSMutableArray array];
        
    }
    
    return _dataTowSource;
    
}


- (NSMutableArray *)dataThreeSource{
    
    if (_dataThreeSource == nil) {
        self.dataThreeSource = [NSMutableArray array];
    }
    
    return _dataThreeSource;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"评论内容";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self  setNetwork];
    [self setUpSuperView];
  
}





- (void)setNetwork{
    
  
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=templateDetial&id=%@",self.ID];
    
    NSLog(@"%@  %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {

            [self.dataSource removeAllObjects];
            [self.dataTowSource removeAllObjects];
            [self.dataThreeSource removeAllObjects];

            NSDictionary *t = [Obj valueForKey:@"t"];
            
            NSArray *arr = [t valueForKey:@"goodResult"];//好成绩
             NSArray *arrTow = [t valueForKey:@"goodHobby"];//好习惯
             NSArray *arrThree = [t valueForKey:@"goodLiteracy"];//好素养
            
            for (NSDictionary *dict  in arr) {
                ZYCommentContentModel *model = [ZYCommentContentModel  new];
                model.ID = [dict valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSource addObject:model];
            }
            
            for (NSDictionary *dictTow  in arrTow) {
                ZYCommentContentTowModel *model = [ZYCommentContentTowModel  new];
                model.ID = [dictTow valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dictTow];
                [self.dataTowSource addObject:model];
            }
            
            for (NSDictionary *dictThree  in arrThree) {
                ZYCommentContentThreeModel *model = [ZYCommentContentThreeModel  new];
                model.ID = [dictThree valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dictThree];
                [self.dataThreeSource addObject:model];
            }
            

           
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
        }
        
    } Fail:^(NSError *Error) {
         NSLog(@"%@",Error);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
       
    }];
    
    
    
}















- (void)setUpSuperView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 20, kScreenHeight - 44)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.estimatedRowHeight = 200;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ZYCommentContentCell  class] forCellReuseIdentifier:kCell];
    [self.tableView registerClass:[ZYCommentContentTowCell  class] forCellReuseIdentifier:kCelll];
    [self.tableView registerClass:[ZYCommentContentThreeCell  class] forCellReuseIdentifier:kCellll];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth, 15)];
    
    
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableHeaderView = view;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (0 == section) {
        
        return self.dataSource.count;
    }else if (1 == section){
        
        return self.dataTowSource.count;
    }else{
        return  self.dataThreeSource.count;
    }
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    

    
    if (0 == indexPath.section) {
        
        
        ZYCommentContentCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
        cell.contentLabel.text = self.str;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.deleteButton.tag = 201;
        cell.updateButton.tag = 301;
        if (self.dataSource.count > indexPath.row) {
            
            ZYCommentContentModel *model = self.dataSource[indexPath.row];
            [cell assignByZYCommentContentModel:model];

        }
        
        
        if (0 == [self.oid integerValue]) {
            
            cell.deleteButton.hidden = YES;
            cell.updateButton.hidden = YES;
            
        }else{
            
            cell.deleteButton.hidden = NO;
            cell.updateButton.hidden = NO;
            
            [cell.deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
            
            [cell.updateButton addTarget:self action:@selector(updateButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        return cell;
    }else if (1 == indexPath.section){
        ZYCommentContentTowCell *cell = [tableView dequeueReusableCellWithIdentifier:kCelll forIndexPath:indexPath];
        cell.contentLabel.text = self.str;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.deleteButton.tag = 202;
        if (self.dataTowSource.count > indexPath.row) {
            ZYCommentContentTowModel *model =self.dataTowSource[indexPath.row];
           
            [cell assignByZYCommentContentTowModel:model];
        }
        
        if (0 == [self.oid integerValue]) {
            
            cell.deleteButton.hidden = YES;
            cell.updateButton.hidden = YES;
            
        }else{
            
            cell.deleteButton.hidden = NO;
            cell.updateButton.hidden = NO;
            
            [cell.deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
            
            [cell.updateButton addTarget:self action:@selector(updateButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
        }
        cell.updateButton.tag = 302;

        
        return cell;
    }else{
        ZYCommentContentThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellll forIndexPath:indexPath];
        cell.contentLabel.text = self.str;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.deleteButton.tag = 203;
        if (self.dataThreeSource.count > indexPath.row) {
            
            ZYCommentContentThreeModel *model = self.dataThreeSource[indexPath.row];
            
             [cell assignByZYCommentContentThreeModel:model];
        }
        if (0 == [self.oid integerValue]) {
            
            cell.deleteButton.hidden = YES;
            cell.updateButton.hidden = YES;
            
        }else{
            
            cell.deleteButton.hidden = NO;
            cell.updateButton.hidden = NO;
            
            [cell.deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
            
            [cell.updateButton addTarget:self action:@selector(updateButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
        }        cell.updateButton.tag = 303;

        
        
        return cell;
    }
    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 35;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    
    if (0 == [self.oid integerValue]) {
        
        return 20;
    }else{
        
       return 60;
    }
    
    
    
    
    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]init];
     headerView.backgroundColor = [UIColor whiteColor];
    // headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    
    if (0 == [self.oid integerValue]) {
       
        UILabel *label = [[UILabel alloc]init];
        
        label.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        
        label.frame = CGRectMake(0, 0, kScreenWidth, 20);
        
        [headerView addSubview:label];
        
    }else{
        
       
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button setTitle:@"+   添加要素" forState:(UIControlStateNormal)];
        [button setTitleColor:UIColor(49, 204, 131) forState:(UIControlStateNormal)];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        
        button.frame = CGRectMake(0, 0, kScreenWidth, 40);
        
        [headerView addSubview:button];
        
        UILabel *label = [[UILabel alloc]init];
        
        label.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        
        label.frame = CGRectMake(0, button.frame.origin.y + button.frame.size.height, button.frame.size.width, 20);
        
        [headerView addSubview:label];
        
        if (0 ==  section) {
            button.tag = 201;
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }else if (1 ==section){
            button.tag = 202;
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }else{
            button.tag = 203;
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }
 
        
        
    }
    
    
    
    
    
    
    
    
    return headerView;
}


//返回每组头部view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [[UIView alloc]init];
    
    //    headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]init];
    
    label.textColor = UIColor(102, 102, 102);
    
    label.font = [UIFont systemFontOfSize:15];
    
    label.frame = CGRectMake(15, 0, 100, 30);
    
    [headerView addSubview:label];
    
    label.text = @"类型";
    
    
    UILabel *labelTow = [[UILabel alloc]init];
    
    labelTow.textColor = UIColor(51, 51, 51);
    
    labelTow.font = [UIFont systemFontOfSize:15];
    
    labelTow.frame = CGRectMake(kScreenWidth - 80, 0, 60, 30);
    
    [headerView addSubview:labelTow];
    
    if (0 == section) {
        labelTow.text = @"好成绩";
    }else if (1 == section){
        labelTow.text = @"好习惯";
    }else{
        labelTow.text = @"好素养";
    }
    
    
    UILabel *labelThree = [[UILabel alloc]init];
    
    labelThree.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    labelThree.frame = CGRectMake(0, 34, kScreenWidth, 1);
    
    [headerView addSubview:labelThree];
    
    
    
    return headerView;
    
}




- (void)buttonAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    ZYAddEssentialController *view = [ZYAddEssentialController new];
    view.templateId = self.ID;
    //type  1为好习惯   2为好成绩  3好素养
    if (201 == tag) {
        view.upDateId = @"2";
    }else if (202 == tag){
        
        view.upDateId = @"1";
    }else{
        
        view.upDateId = @"3";
    }
    
    
    
   
    
    view.backBlock = ^(NSString *str) {
        
        [self setNetwork];
    };
    
    
    
    [self.navigationController pushViewController:view animated:YES];
    
    
    //    [self.tableView reloadData];
    
    
}
#pragma mark ---- 删除方法的实现
- (void)deleteButtonAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    if (201 == tag) {
        ZYCommentContentCell *cell = (ZYCommentContentCell *)sender.superview.superview;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        ZYCommentContentModel *model = self.dataSource[indexPath.row];


        ZYalertController *alert = [ZYalertController alertControllerWithTitle:@"温馨提示" message:@"是否删除?" preferredStyle:(UIAlertControllerStyleAlert)];
        ZYAlertAction *action = [ZYAlertAction actionWithTitle:@"YES" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

            if (self.dataSource.count > 1) {


                NSMutableArray *temp = [NSMutableArray arrayWithArray:self.dataSource];

                [temp removeObjectAtIndex:indexPath.row];

                self.dataSource = temp;

                NSArray *deleteItems = @[indexPath];

                [self.tableView deleteRowsAtIndexPaths:deleteItems withRowAnimation:(UITableViewRowAnimationRight)];

            }else{
                [self.dataSource removeAllObjects];
                [self.tableView reloadData];
            }


             [self netMoonWorkId:[NSString stringWithFormat:@"%@",model.ID]];


        }];

        ZYAlertAction *actionn = [ZYAlertAction actionWithTitle:@"NO" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

        }];

        action.textColor = [UIColor blackColor];
        actionn.textColor = [UIColor blackColor];
        [alert addAction:action];
        [alert addAction:actionn];
        [self presentViewController:alert animated:YES completion:nil];



        //[self deleteId:model.ID  dataSource:self.dataSource indexPath:indexPath];
    }else if (202 == tag){
        NSLog(@"fffff");
        ZYCommentContentTowCell *cellTow = (ZYCommentContentTowCell *)sender.superview.superview;
        NSIndexPath *indexPathTow = [self.tableView indexPathForCell:cellTow];
        ZYCommentContentTowModel *modelTow =self.dataTowSource[indexPathTow.row];

        ZYalertController *alert = [ZYalertController alertControllerWithTitle:@"温馨提示" message:@"是否删除?" preferredStyle:(UIAlertControllerStyleAlert)];
        ZYAlertAction *action = [ZYAlertAction actionWithTitle:@"YES" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

            if (self.dataTowSource.count > 1) {


                NSMutableArray *temp = [NSMutableArray arrayWithArray:self.dataTowSource];

                [temp removeObjectAtIndex:indexPathTow.row];

                self.dataTowSource = temp;

                NSArray *deleteItems = @[indexPathTow];

                [self.tableView deleteRowsAtIndexPaths:deleteItems withRowAnimation:(UITableViewRowAnimationRight)];

            }else{
                [self.dataTowSource removeAllObjects];
                [self.tableView reloadData];
            }


             [self netMoonWorkId:[NSString stringWithFormat:@"%@",modelTow.ID]];


        }];

        ZYAlertAction *actionn = [ZYAlertAction actionWithTitle:@"NO" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

        }];

        action.textColor = [UIColor blackColor];
        actionn.textColor = [UIColor blackColor];
        [alert addAction:action];
        [alert addAction:actionn];
        [self presentViewController:alert animated:YES completion:nil];



        // [self deleteId:modelTow.ID  dataSource:self.dataTowSource indexPath:indexPathTow];
    }else{
        
        NSLog(@"sss");
        ZYCommentContentThreeCell *cellThree = (ZYCommentContentThreeCell *)sender.superview.superview;
        NSIndexPath *indexPathThree = [self.tableView indexPathForCell:cellThree];
        ZYCommentContentThreeModel *modelThree = self.dataThreeSource[indexPathThree.row];


        ZYalertController *alert = [ZYalertController alertControllerWithTitle:@"温馨提示" message:@"是否删除?" preferredStyle:(UIAlertControllerStyleAlert)];
        ZYAlertAction *action = [ZYAlertAction actionWithTitle:@"YES" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

            if (self.dataThreeSource.count > 1) {


                NSMutableArray *temp = [NSMutableArray arrayWithArray:self.dataThreeSource];

                [temp removeObjectAtIndex:indexPathThree.row];

                self.dataThreeSource = temp;

                NSArray *deleteItems = @[indexPathThree];

                [self.tableView deleteRowsAtIndexPaths:deleteItems withRowAnimation:(UITableViewRowAnimationRight)];

            }else{
                [self.dataThreeSource removeAllObjects];
                [self.tableView reloadData];
            }


            [self netMoonWorkId:[NSString stringWithFormat:@"%@",modelThree.ID]];


        }];

        ZYAlertAction *actionn = [ZYAlertAction actionWithTitle:@"NO" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

        }];

        action.textColor = [UIColor blackColor];
        actionn.textColor = [UIColor blackColor];
        [alert addAction:action];
        [alert addAction:actionn];
        [self presentViewController:alert animated:YES completion:nil];




        // [self deleteId:modelThree.ID  dataSource:self.dataThreeSource indexPath:indexPathThree];
    }
    
  
    
   
    
    
    
}



#pragma mark --- 删除接口的实现
- (void)netMoonWorkId:(NSString *)netMoonWorkId{
//    http://192.168.0.113:8080/StarShine/ys/yxhUser.do?action=deleteTemplateContent&id=48
    
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=deleteTemplateContent&id=%@",netMoonWorkId];
    
    
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        
    } Fail:^(NSError *Error) {
        
    }];
}






#pragma mark --- 修改方法的实现
- (void)updateButtonAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;

    ZYAddEssentialController *vc = [ZYAddEssentialController new];
    vc.isUpDate = YES;
    
    
    if (301 == tag) {
        ZYCommentContentCell *cell = (ZYCommentContentCell *)sender.superview.superview;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        ZYCommentContentModel *model = self.dataSource[indexPath.row];
        
        
        vc.upDateId = [NSString stringWithFormat:@"%@",model.type];
        vc.ID = [NSString stringWithFormat:@"%@",model.ID];
        
    
        vc.backBlock = ^() {
            
            [self setNetwork];
        };
        
        
        [self.navigationController pushViewController:vc animated:YES];
        
        

        
        
        
        
    }else if (302 == tag){
        ZYCommentContentTowCell *cellTow = (ZYCommentContentTowCell *)sender.superview.superview;
        NSIndexPath *indexPathTow = [self.tableView indexPathForCell:cellTow];
        ZYCommentContentTowModel *modelTow =self.dataTowSource[indexPathTow.row];
        
        vc.upDateId = [NSString stringWithFormat:@"%@",modelTow.type];
        vc.ID = [NSString stringWithFormat:@"%@",modelTow.ID];
        
        vc.backBlock = ^() {
            
            [self setNetwork];
        };
        
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
        
    }else{
        ZYCommentContentThreeCell *cellThree = (ZYCommentContentThreeCell *)sender.superview.superview;
        NSIndexPath *indexPathThree = [self.tableView indexPathForCell:cellThree];
        ZYCommentContentThreeModel *modelThree = self.dataThreeSource[indexPathThree.row];
        
        vc.upDateId = [NSString stringWithFormat:@"%@",modelThree.type];
        vc.ID = [NSString stringWithFormat:@"%@",modelThree.ID];
        
        vc.backBlock = ^() {
            
              [self setNetwork];
        };
        
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
    
    
    
    
    
    
    
    
}







- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];
}







@end
